#ifndef __Led_C
#define __Led_C

#include "Led.h"

#if I_rated_count >  1
		#ifdef  Led3Pin_MCUPin
		//对应Led灯列表;
		uint16_t Led_idx_List[I_rated_count]      = { Led1_PinId,  Led2_PinId,  Led3_PinId };  
		#elif CHGR_panel_Sel == CHGR_panel_B14
		//不同rated电流,对应显示不同的595位码;
		uint8_t Led595_List[I_rated_count]        = { 0xf0,        0xe8,        0xd8 };  
		#elif CHGR_panel_Sel == CHGR_panel_A12 || CHGR_panel_Sel == CHGR_panel_B20    
		//不同rated电流,对应显示不同的595位码=亮不同位置的灯
		uint8_t Led595_List[I_rated_count]        = { 0xf0,        0xe8,        0xd8 };   
		#endif
#endif

void led_init(void){
		GPIO_Config_T  configStruct;
    RCM_EnableAPB2PeriphClock(Led_RTC);
	  // 按键灯RTM设置
	  #if Led_Key_Ctrl == 1
		RCM_EnableAPB2PeriphClock(Led_Key_RTC);		
		#endif
	
	  ///////////////// 灯引脚配置 /////////////////////		
	  // 灯板的灯
	  #ifdef Using_LedCtrl_MCUPin
			configStruct.pin = Led_All;	  
	  #elif defined Using_LedCtrl_74HC595
			configStruct.pin = BL_PinId | SDA_PinId | SCLK_PinId | RCLK_PinId;
    #endif
    configStruct.mode = GPIO_MODE_OUT_PP;
    configStruct.speed = GPIO_SPEED_50MHz;
    GPIO_Config(Led_PinPort, &configStruct);
	  // 按键灯	  
	  #if Led_Key_Ctrl == 1
			configStruct.pin = Led_Key_PinId;	
			configStruct.mode = GPIO_MODE_OUT_PP;
			configStruct.speed = GPIO_SPEED_50MHz;  
			GPIO_Config(Led_Key_PinPort, &configStruct);	
		#endif
		  
	  ///////////////////  默认Ledoff	 ////////////////////
	  #ifdef Using_LedCtrl_74HC595 
	  led_off(BL_PinId); 
	  led_bit_reset(SDA_PinId | SCLK_PinId | RCLK_PinId); 
  	#elif defined Using_LedCtrl_MCUPin 
	  Led_PinPort -> BSC = Led_All; 
    #endif	
		
    // 按键灯:		
	  #if Led_Key_Ctrl == 1
		Led_Key_PinPort -> BC = Led_Key_PinId; 
		#endif
			
}

#if defined Using_LedCtrl_74HC595
#if HC595Dri_FastMode == 0
// 发送亮灯字节
void Led595_start(uint8_t bitData){
	  uint8_t i = 0;
	  led_bit_reset(RCLK_PinId);
	  led_bit_reset(SCLK_PinId);
	  delaynus(1);
    for(i = 0; i < 8; i++){
        if(bitData & 0x01){
					  led_bit_set(SDA_PinId);
				} else {
					  led_bit_reset(SDA_PinId);
				}
				delaynus(1);
        bitData = bitData >> 1;	
        led_bit_set(SCLK_PinId);   // 上升沿，数据移位
				delaynus(1);
				led_bit_reset(SCLK_PinId); // 拉低，等待下次上升沿使用
				delaynus(1);
    }
		led_bit_set(RCLK_PinId); //上升沿8位数据并行输出
		delaynus(1);
		led_bit_reset(RCLK_PinId);
		delaynus(1);
}
#else
// 发送亮灯字节
void Led595_start(uint8_t bitData){
	  uint8_t i = 0;
	  led_bit_reset(RCLK_PinId);
	  led_bit_reset(SCLK_PinId);
    for(i = 0; i < 8; i++){
        if(bitData & 0x01){
					  led_bit_set(SDA_PinId);
				} else {
					  led_bit_reset(SDA_PinId);
				}
        bitData = bitData >> 1;	
        led_bit_set(SCLK_PinId); // 上升沿，数据移位
				led_bit_reset(SCLK_PinId); // 拉低，等待下次上升沿使用
    }
		led_bit_set(RCLK_PinId); //上升沿8位数据并行输出
		led_bit_reset(RCLK_PinId);
}
#endif

#endif

#endif


